import numpy as np
from matplotlib.pyplot import plot,show

def fit_line(t,y):
	A=np.vstack([t,np.ones_like(t)]).T
	return np.linalg.lstsq(A,y)[0]

h,l,c=np.loadtxt('d:/111.csv',delimiter=',',usecols=(2,3,4),unpack=True)
h=h[:30]
l=l[:30]
c=c[:30]

pivots=(h+l+c)/3
print("Pivots",pivots)

t=np.arange(len(c))
sa,sb=fit_line(t,pivots-(h-l))
ra,rb=fit_line(t,pivots+(h-l))
support=sa*t+sb
resistance=ra*t+rb

condition=(c>support)&(c<resistance)
print("Condition",condition)
between_bands=np.where(condition)
print(support[between_bands])
print(c[between_bands])
print(resistance[between_bands])
between_bands=len(np.ravel(between_bands))
print("Number points between bands",between_bands)
print("Ratio between bands",float(between_bands)/len(c))

print("Tomorrows support",sa*(t[-1]+1)+sb)
print("Tomorrows resistance",ra*(t[-1]+1)+rb)

a1=c[c>support]
a2=c[c<resistance]
print("Number of points between bands 2nd approach",len(np.intersect1d(a1,a2)))

plot(t,c)
plot(t,support)
plot(t,resistance)
show()